View Javadoc
1   package org.apache.maven.surefire.testng.conf;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Method;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.maven.surefire.booter.ProviderParameterNames;
28  import org.apache.maven.surefire.testset.TestSetFailedException;
29  import org.testng.TestNG;
30  import org.testng.xml.XmlSuite;
31  
32  /**
33   * TestNG configurator for 5.3+ versions. TestNG exposes a {@link org.testng.TestNG#configure(java.util.Map)} method.
34   * All suppported TestNG options are passed in String format, except
35   * <code>TestNGCommandLineArgs.LISTENER_COMMAND_OPT</code> which is <code>List&gt;Class&lt;</code>,
36   * <code>TestNGCommandLineArgs.JUNIT_DEF_OPT</code> which is a <code>Boolean</code>,
37   * <code>TestNGCommandLineArgs.SKIP_FAILED_INVOCATION_COUNT_OPT</code> which is a <code>Boolean</code>,
38   * <code>TestNGCommandLineArgs.OBJECT_FACTORY_COMMAND_OPT</code> which is a <code>Class</code>,
39   * <code>TestNGCommandLineArgs.REPORTERS_LIST</code> which is a <code>List&gt;ReporterConfig&lt;</code>.
40   * <p/>
41   * Test classes and/or suite files are not passed along as options parameters, but configured separately.
42   *
43   * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
44   */
45  public class TestNGMapConfigurator
46      implements Configurator
47  {
48      public void configure( TestNG testng, Map<String, String> options )
49          throws TestSetFailedException
50      {
51          Map convertedOptions = getConvertedOptions( options );
52          testng.configure( convertedOptions );
53      }
54  
55      public void configure( XmlSuite suite, Map<String, String> options )
56          throws TestSetFailedException
57      {
58          String threadCountString = options.get( ProviderParameterNames.THREADCOUNT_PROP );
59          int threadCount = ( null != threadCountString ) ? Integer.parseInt( threadCountString ) : 1;
60          suite.setThreadCount( threadCount );
61  
62          String parallel = options.get( ProviderParameterNames.PARALLEL_PROP );
63          if ( parallel != null )
64          {
65              suite.setParallel( parallel );
66          }
67      }
68  
69      Map<String, Object> getConvertedOptions( Map<String, String> options )
70          throws TestSetFailedException
71      {
72          Map<String, Object> convertedOptions = new HashMap<String, Object>();
73          convertedOptions.put( "-mixed", false );
74          for ( Map.Entry<String, String> entry : options.entrySet() )
75          {
76              String key = entry.getKey();
77              Object val = entry.getValue();
78              if ( "listener".equals( key ) )
79              {
80                  val = AbstractDirectConfigurator.loadListenerClasses( entry.getValue() );
81              }
82              else if ( "objectfactory".equals( key ) )
83              {
84                  val = AbstractDirectConfigurator.loadClass( entry.getValue() );
85              }
86              else if ( "testrunfactory".equals( key ) )
87              {
88                  val = AbstractDirectConfigurator.loadClass( entry.getValue() );
89              }
90              else if ( "reporter".equals( key ) )
91              {
92                  // TODO support multiple reporters?
93                  val = convertReporterConfig( val );
94                  key = "reporterslist";
95              }
96              else if ( "junit".equals( key ) )
97              {
98                  val = convert( val, Boolean.class );
99              }
100             else if ( "skipfailedinvocationcounts".equals( key ) )
101             {
102                 val = convert( val, Boolean.class );
103             }
104             else if ( "mixed".equals( key ) )
105             {
106                 val = convert( val, Boolean.class );
107             }
108             else if ( "configfailurepolicy".equals( key ) )
109             {
110                 val = convert( val, String.class );
111             }
112             else if ( "group-by-instances".equals( key ) )
113             {
114                 val = convert( val, Boolean.class );
115             }
116             else if ( ProviderParameterNames.THREADCOUNT_PROP.equals( key ) )
117             {
118                 val = convert ( val, String.class );
119             }
120             else if ( "suitethreadpoolsize".equals( key ) )
121             {
122                 // for TestNG 6.9.7 or higher
123                 val = convert( val, Integer.class );
124             }
125             else if ( "dataproviderthreadcount".equals( key ) )
126             {
127                 // for TestNG 5.10 or higher
128                 val = convert( val, Integer.class );
129             }
130 
131             if ( key.startsWith( "-" ) )
132             {
133                 convertedOptions.put( key, val );
134             }
135             else
136             {
137                 convertedOptions.put( "-" + key, val );
138             }
139         }
140         return convertedOptions;
141     }
142 
143     // ReporterConfig only became available in later versions of TestNG
144     private Object convertReporterConfig( Object val )
145     {
146         final String reporterConfigClassName = "org.testng.ReporterConfig";
147         try
148         {
149             Class<?> reporterConfig = Class.forName( reporterConfigClassName );
150             Method deserialize = reporterConfig.getMethod( "deserialize", String.class );
151             Object rc = deserialize.invoke( null, val );
152             ArrayList<Object> reportersList = new ArrayList<Object>();
153             reportersList.add( rc );
154             return reportersList;
155         }
156         catch ( Exception e )
157         {
158             return val;
159         }
160     }
161 
162     protected Object convert( Object val, Class<?> type )
163     {
164         if ( val == null )
165         {
166             return null;
167         }
168         if ( type.isAssignableFrom( val.getClass() ) )
169         {
170             return val;
171         }
172 
173         if ( ( type == Boolean.class || type == boolean.class ) && val.getClass() == String.class )
174         {
175             return Boolean.valueOf( (String) val );
176         }
177 
178         if ( ( type == Integer.class || type == int.class ) && val.getClass() == String.class )
179         {
180             return Integer.valueOf( (String) val );
181         }
182 
183         if ( type == String.class )
184         {
185             return val.toString();
186         }
187 
188         return val;
189     }
190 }